﻿using CPPEI.Coolzon.Designtime;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// ScriptConfig.xaml 的交互逻辑
    /// </summary>
    public partial class ScriptConfig : Window
    {
        private Designtime.Metadata.Scene _scene;
        private ObservableCollection<Designtime.Metadata.Script> _scripts;
        public ScriptConfig(Designtime.Metadata.Scene scene)
        {
            InitializeComponent();
            _scene = scene;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _scripts = new ObservableCollection<Designtime.Metadata.Script>();
            foreach (var script in _scene.GetScripts())
            {
                _scripts.Add(script);
            }
            _ctlListView.ItemsSource = _scripts;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < _scripts.Count; i++)
            {
                Designtime.Metadata.Script script = _scripts[i];
                if (string.IsNullOrWhiteSpace(script.Source))
                {
                    CustomMessageBox.ShowWarning("请选择脚本");
                    return;
                }
                for (int j = i + 1; j < _scripts.Count; j++)
                {
                    if (string.Equals(script.Source, _scripts[j].Source))
                    {
                        CustomMessageBox.ShowWarning("请不要选择相同脚本");
                        return;
                    }
                }
            }
            if (UIContext.Current.CurrentSelectedNode != null && UIContext.Current.CurrentSelectedNode.NodeStatus == NodeStatus.CheckOut)
            {
                SaveScripts();
                DialogResult = true;
            }
            else
            {
                List<SolutionTreeViewNode> nodeList = new List<SolutionTreeViewNode>();
                nodeList.Add(UIContext.Current.CurrentSelectedNode);
                CheckOutThread.Start(nodeList, ReportProgress, Completed);
            }
        }

        private void Completed(bool hasError, int success, int total)
        {
            if (hasError)
            {
                Dispatcher.Invoke(() =>
                {
                    CustomMessageBox.ShowWarning("签出场景失败");
                });
            }
            else
            {
                SaveScripts();
                Dispatcher.Invoke(() =>
                {
                    DialogResult = true;
                });
            }
        }

        private void ReportProgress(Guid arg1, string arg2, int arg3, int arg4)
        {

        }

        private void SaveScripts()
        {
            _scene.SaveScripts(_scripts);
            Designtime.Metadata.LocalStorage.SaveSceneFile(UIContext.Current.LocalWorkSpace.LocalPath, UIContext.Current.CurrentSolution.ID, _scene);
        }

        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            Designtime.Metadata.Script script = new Designtime.Metadata.Script("", "");
            _scripts.Add(script);
        }

        private void _ctlRemove_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlListView.SelectedItem != null)
            {
                _scripts.Remove(_ctlListView.SelectedItem as Designtime.Metadata.Script);
            }
        }

        private void ComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            Designtime.Metadata.Script script = cb.Tag as Designtime.Metadata.Script;
            Uri fullPath = null;
            if (script != null && !string.IsNullOrWhiteSpace(script.Source))
            {
                fullPath = new Uri(SolutionResourceManager.GetAbsoluteFilePath(script.Source), UriKind.Absolute);
            }
            ImageRes selected = null;
            ImageRes[] imageList = ResPoolFactory.GetImageResPool().GetScripts();
            if (imageList != null && imageList.Length > 0)
            {
                foreach (var item in imageList)
                {
                    cb.Items.Add(item);
                    if (fullPath != null && fullPath.AbsolutePath == new Uri(item.FullPath).AbsolutePath)
                    {
                        selected = item;
                    }
                }
                if (selected != null)
                {
                    cb.SelectedItem = selected;
                }
            }
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            if (cb.SelectedItem != null)
            {
                ImageRes imageRes = cb.SelectedItem as ImageRes;
                Designtime.Metadata.Script script = cb.Tag as Designtime.Metadata.Script;
                script.Source = SolutionResourceManager.GetRelativeFilePath(imageRes.FullPath);
            }
        }

        private void Element_GotFocus(object sender, RoutedEventArgs e)
        {
            var aa = (sender as FrameworkElement).DataContext;
            _ctlListView.SelectedItem = aa;
        }
    }
}
